package StuManagerSystem.controller;

import StuManagerSystem.bean.Admin;
import StuManagerSystem.bean.Person;
import StuManagerSystem.bean.Student;
import StuManagerSystem.database.AdminData;
import StuManagerSystem.database.StudentData;
import StuManagerSystem.database.TeacherData;
import StuManagerSystem.service.AdminService;
import StuManagerSystem.service.LoginService;
import StuManagerSystem.service.StudentService;

import java.util.List;
import java.util.Scanner;

public class View {
	private static List<Person> studentList = StudentData.data();
	private static List<Person> adminList = AdminData.data();
	private static List<Person> teacherList = TeacherData.data();
	
	protected static String entrance() {

		System.out.println("***************************************************");
		System.out.println("\t\t欢迎使用学生管理系统");
		System.out.println("\t\t1.学生\t2.教师\t3.管理员\t4退出");

		String info = LoginService.login();

		String[] str = info.split(",");
		String number = str[0];
		String username = str[1];
		String password = str[2];
		if ("1".equals(number)) {
			for (int i = 0; i < studentList.size(); i++) {
				if (username.equals(studentList.get(i).getUsername())) {
					if (password.equals(studentList.get(i).getPassword())) {
						studentView(studentList.get(i));
						entrance();
					}
				}
			}
			System.out.println("\t\t账号密码错误！");
			entrance();

		} else if ("2".equals(number)) {
			
		} else if ("3".equals(number)) {
			for (int i = 0; i < adminList.size(); i++) {
				if (username.equals(adminList.get(i).getUsername())) {
					if (password.equals(adminList.get(i).getPassword())) {
						adminView(adminList.get(i));
						entrance();
					}
				}
			}
			System.out.println("账号密码错误！");
			LoginService.login();
		}else {
			return "\t\t登录出错...";
		}

		return "\t\t感谢您使用学生管理系统，再见！";

	}

	private static void studentView(Person stu) {
		Scanner sc = new Scanner(System.in);
		System.out.println("\t\t学生界面");
		System.out.println("\t\t欢迎您，" + ((Student)stu).getUsername());
		while (true) {
			System.out.println("\t\t1.查看成绩\t2.查看信息\t3.修改密码\t4.退出");
			System.out.print("\t\t请选择功能：");
			Integer number = sc.nextInt();
			if (number == 1) {
				StudentService.checkScore((Student)stu);
			} else if (number == 2) {
				StudentService.checkInfo((Student)stu);
			}else if(number ==3) {
				boolean result = StudentService.updatePassword(((Student)stu).getNumber(),studentList);
				if(result) {
					System.out.println("\t\t修改成功！");
				}else {
					System.out.println("\t\t修改失败！");
				}
			}else if (number == 4) {
				//退出jvm
				System.exit(0);
			} else {
				return;
			}
		}

	}

	
	private static void adminView(Person admin) {
		Admin adm = (Admin) admin;
		Scanner sc = new Scanner(System.in);
		System.out.println("\t\t管理员界面");
		System.out.println("\t\t欢迎您，" + admin.getUsername());
		System.out.println("\t\t1.添加学生信息\t2.查看学生信息\t3.修改学生信息\t4.删除学生信息\n"
				+ "\t\t5.添加教师信息\t6.查看教师信息\t7.修改教师信息\t8.删除教师信息\n"
				+ "\t\t9.退出");
		while (true) {
			System.out.print("\t\t请选择操作功能：");
			Integer num = sc.nextInt();
			switch (num) {
			case 1:
				// 添加学生
				if(AdminService.addStudent(studentList)) {
					System.out.println("\t\t添加成功！");
				}else {
					System.out.println("\t\t添加失败！");
				}
				System.out.println("***************************************************");
				break;
			case 2:
				//查看学生
				AdminService.checkAllStudent(studentList);
				System.out.println("***************************************************");
				break;
			case 3:
				//修改学生信息
				if(AdminService.updateStudent(studentList)) {
					System.out.println("\t\t修改成功！");
				}else {
					System.out.println("\t\t修改失败！");
				}
				System.out.println("***************************************************");
				break;
			case 4:
				//删除学生信息
				if(AdminService.deleteStudent(studentList)) {
					System.out.println("\t\t删除成功！");
				}else {
					System.out.println("\t\t修改失败！");
				}
				System.out.println("***************************************************");
				break;
			case 5:
				//添加教师信息
				if(AdminService.addTeacher(teacherList)) {
					System.out.println("\t\t添加成功！");
				}else {
					System.out.println("\t\t添加失败！");
				}
				System.out.println("***************************************************");
				break;
			case 6:
				//查看教师信息
				AdminService.checkAllTeacher(teacherList);
				System.out.println("***************************************************");
				break;
			case 7:
				//修改教师信息
				AdminService.updateTeacher(teacherList);
				System.out.println("***************************************************");
				break;
			case 8:
				//删除教师信息
				if(AdminService.deleteTeacher(teacherList)) {
					System.out.println("删除成功！");
				}else {
					System.out.println("删除失败！");
				}
				System.out.println("***************************************************");
				break;
			case 9:
				return;
			}
		}
	}

}
